window.CascadeData = function(){

    this.ZT = [];
    this.QY = [];
    this.YT = [] ;

    this.data = {};
    this.url = null;

    this.Path = "";

    this.name = "condition_data";

    this.result = null;

};

CascadeData.prototype  = {

    init:function( url ){
        this.url = url;
        this.Path = "/Public/sys/lib/work/cascade.work.js";

        var data  = window.sessionStorage[ "condition_data" ];

        if(!data){ this.ajax(); }
    },

    askdata:function( option ,callback )
    {
        if(!this.url) return null;

        var data  = window.sessionStorage[ "condition_data" ];

        if(data){
            this.success(data,option,callback);
        }else{
           this.ajax(option,callback);
        }

    },
    ajax:function(option ,callback){

        $.ajax({
            url:this.url,
            type:"post",
            context:this,
            async: false,
            success:function( _data ){
                this.success(_data,option ,callback);
            }
        });
    },
    success:function( data ,option ,callback){

        if(!window.sessionStorage["condition_data"])window.sessionStorage.setItem("condition_data",data);

        var _this = this ;

        if(!option) return ;

        if(option["QY"]==this.QY && option["ZT"]==this.ZT && option["YT"]==this.YT){
            if(!this.result){
                callback(this.result);
                return ;
            }
        }

        this.setOption(option);

        var oWorker = new Worker( this.Path );

        if(typeof data =="string")data = JSON.parse(data);

        oWorker.postMessage( { d:data,t:this} );

        oWorker.onmessage = function( e )
        {
            _this.result = e.data["result"];

            if(typeof  callback  == "function")callback( _this.result );
        };

    },
    reset:function(){
        this.QY = [];
        this.ZT = [];
        this.YT = [];
    },
    setOption:function(option){
        this.reset();
        if(option)for(var i in option)this[i] = option[i];
    },

    decode:function (s) {
        return unescape(s.replace(/\\(u[0-9a-fA-F]{4})/gm, '%$1'));
    }
};
